'use strict'

const config = require('./config')
const Wechat = require('./wechat/wechat')

const wechatApi = new Wechat(config.wechat)

exports.reply = function* (next) {
  let message = this.weixin
  if (message.msgType === 'event') {
    if (message.Event === 'subscribe') {
      if (message.EventKey) {
        console.log(`扫二维码进来：${message.EventKey} ${message.ticket}`)
      }
      this.body = '`哈哈，你订阅了这个公众号'
    } else if (message.Event === 'unsubscribe') {
      this.body = ''
    }
  } else if (message.MsgType === 'text') {
    let content = message.Content
    let reply = `额，你说的${content}太复杂了`

    if (content === '1') {
      reply = '天下第一吃大米'
    } else if (content === '2') {
      reply = '天下第二吃豆腐'
    } else if (content === '3') {
      reply = '天下第三吃仙丹'
    } else if (content === '4') {
        reply = [{
          title: '技术改变世界',
          description: '只是个描述而已',
          picUrl: 'http://img.zcool.cn/community/0117e2571b8b246ac72538120dd8a4.jpg@1280w_1l_2o_100sh.jpg',
          url: 'https://www.baidu.com/'
        },{
          title: 'node.js很牛逼',
          description: '爽到爆',
          picUrl: 'http://img.zcool.cn/community/0117e2571b8b246ac72538120dd8a4.jpg@1280w_1l_2o_100sh.jpg',
          url: 'https://www.baidu.com/'
        }]
    } else if (content === '5') {
      let data = yield wechatApi.uploadMaterial('image', __dirname + '/wechat/2.jpg')
      reply = {
        type: 'image',
        mediaId: data.media_id
      }
    } else if (content === '6') {
      let data = yield wechatApi.uploadMaterial('video', __dirname + '/wechat/6.mp4')
      reply = {
        type: 'video',
        title: '回复视频内容',
        description: '打个篮球玩玩',
        mediaId: data.media_id
      }
    } else if (content === '7') {
      let data = yield wechatApi.uploadMaterial('image', __dirname + '/wechat/2.jpg')
      reply = {
        type: 'music',
        title: '这是一段音乐',
        description: '这是一段音乐',
        musicUrl: 'https://m10.music.126.net/20181016211926/872037cced6ee4287cf05961f75e64df/ymusic/080b/75ce/8a92/a31dae00021cf0442d8d81e236e97535.mp3',
        thumbMediaId: data.media_id
      }
    } else if (content === '8') {
      let data = yield wechatApi.uploadMaterial('image', __dirname + '/wechat/2.jpg', {type: 'image'})
      reply = {
        type: 'image',
        mediaId: data.media_id
      }
    } else if (content === '9') {
      let data = yield wechatApi.uploadMaterial('video', __dirname + '/wechat/6.mp4', {type: 'video', description: '{"title":"回复视频内容","introduction":"打个篮球玩玩"}'})
      console.log(data)
      reply = {
        type: 'video',
        title: '回复视频内容',
        description: '打个篮球玩玩',
        mediaId: data.media_id
      }
    } else if (content === '10') {
      let picData = yield wechatApi.uploadMaterial('image', __dirname + '/wechat/2.jpg', {})

      let media = {
        articles: [{
          title: 'tututu',
          thumb_media_id: picData.media_id,
          author: 'pan',
          digest: '没有摘要',
          show_cover_pic: 1,
          content: '没有内容',
          content_source_url: 'https://www.baidu.com'
        }]
      }
      console.log(1)
      let data = yield wechatApi.uploadMaterial('news', media, {})
      data = yield wechatApi.fetchMaterial(data.media_id, 'news', {})

      let items = data.news_item
      let news = []
      items.forEach(item => {
        news.push({
          title: item.title,
          description: item.digest,
          picUrl: picData.url,
          url: item.url
        })
      })
      reply = news
    } else if (content === '11') {
      let counts = yield wechatApi.countMaterial()
      console.log(JSON.stringify(counts))

      let results = yield [
        wechatApi.batchMaterial({
          type: 'image',
          offset: 0,
          count: 10
        }),
        wechatApi.batchMaterial({
          type: 'video',
          offset: 0,
          count: 10
        }),
        wechatApi.batchMaterial({
          type: 'voice',
          offset: 0,
          count: 10
        }),
        wechatApi.batchMaterial({
          type: 'news',
          offset: 0,
          count: 10
        })
      ]

      console.log(results)

      reply = '1'
    } else if (content === '12') {
      console.log(message)
      let user = yield wechatApi.batchFetchUsers(message.FromUserName)
    }

    this.body = reply
  }
  yield next
}